<template>
	<view class="content">
		<u-form :model="userInfo" ref="form1" class="user-list list-border">
			<u-form-item label="姓名" prop="name" label-width="140">
				<u-input placeholder="请填写姓名" v-model="userInfo.name" />
			</u-form-item>
			<u-form-item label="性别" prop="sex" label-width="140">
				<u-input v-model="userInfo.sex" placeholder="请选择性别" type="select" @click="select1.show=true" />
			</u-form-item>
			<u-form-item label="身份证" prop="sfid" label-width="140">
				<u-input placeholder="请填写身份证" type="idcard" :maxlength="18" v-model="userInfo.sfid" />
			</u-form-item>
			<u-form-item label="手机号" prop="phone" label-width="140">
				<u-input placeholder="请填写手机号" :maxlength="11" v-model="userInfo.phone" />
			</u-form-item>
			<u-form-item label="出生日期" label-width="140" @click="timeshow=true">
				<view class="">
					<view class="dis" v-if="timeinfor.time!=''">
						<view class="uni-input">{{timeinfor.time}}&emsp;</view>
					</view>
					<view v-else style="color: #b5b5b5;">
						<span>请选择出生日期</span>
					</view>
				</view>
			</u-form-item>
			<u-form-item label="年龄" prop="age" label-width="140">
				<u-input placeholder="请填写手机号" :maxlength="11" v-model="userInfo.age" />
			</u-form-item>
			<u-form-item label="地区" label-width="140" @click="regionshow=true">
				<view class="">
					<view class="dis" v-if="adderssinfor.array!=''">
						<view class="uni-input" v-for="(item,index) in adderssinfor.array" :key="index">{{item}}&emsp;</view>
					</view>
					<view v-else style="color: #b5b5b5;">
						<span>请选择省市县</span>
					</view>
				</view>
			</u-form-item>
			<u-form-item label="小区" prop="xq" label-width="140">
				<u-input placeholder="请填写小区" :maxlength="11" v-model="userInfo.xq" />
			</u-form-item>
			<u-form-item label="楼号" prop="lh" label-width="140">
				<u-input placeholder="请填写楼号" :maxlength="11" v-model="userInfo.lh" />
			</u-form-item>
			<u-form-item label="单元号" prop="dyh" label-width="140">
				<u-input placeholder="请填写单元号" :maxlength="11" v-model="userInfo.dyh" />
			</u-form-item>
			<u-form-item label="房间号" prop="fjh" label-width="140">
				<u-input placeholder="请填写房间号" :maxlength="11" v-model="userInfo.fjh" />
			</u-form-item>
		</u-form>
		<u-picker v-model="timeshow"  mode="time" @confirm="timePickerChange" :default-time="timeinfor.time"></u-picker>
		<u-picker v-model="regionshow"  mode="region" @confirm="bindPickerChange" :default-region="array"></u-picker>
		<u-select v-model="select1.show" :list="select1.list" @confirm="onSelect1"></u-select>
		<view class="bottom-button">
			<!-- <u-button shape="circle" type="primary" size="default" @click="submit">提交</u-button> -->
			<view class="button bacnone2" @click="submit">
				<span>修改</span>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapGetters
	} from 'vuex'

	export default {
		data() {
			return {
				array: ["河北省", "衡水市", "安平县"],
				adderss: '',
				regionshow: false,
				timeshow:false,
				sexs: '请选择',
				show: false,
				timeinfor: {
					time:'',
				},
				adderssinfor: {
					AddressId:'',
					array:["河北省", "衡水市", "安平县"],
					Province:'河北省',
					City:'衡水市',
					County:'安平县',
					name:'',
					phone:'',
					detial:'',
					checked: true
				},
				tips: '',
				value: '',
				userInfo: {
					PatientId:'',
					name: '',
					sex: '男',
					age:'',
					sfid: '',
					phone: '',
					checkbox: '',
					xq:'',
					lh:'',
					dyh:'',
					fjh:''
				},
				select1: {
					show: false,
					current: {},
					list: [{
							value: 1,
							label: '男'
						},
						{
							value: 2,
							label: '女'
						}
					],
				},
				useyzm: '',
				rules: {
					name: [{
						required: true,
						message: '请输入姓名',
						// 可以单个或者同时写两个触发验证方式
						trigger: 'blur,change'
					}],
					lh: [{
						required: true,
						message: '请输入楼号',
						// 可以单个或者同时写两个触发验证方式
						trigger: 'blur,change'
					}],
					xq: [{
						required: true,
						message: '请输入小区',
						// 可以单个或者同时写两个触发验证方式
						trigger: 'blur,change'
					}],
					dyh: [{
						required: true,
						message: '请输入单元号',
						// 可以单个或者同时写两个触发验证方式
						trigger: 'blur,change'
					}],
					fjh: [{
						required: true,
						message: '请输入房间号',
						// 可以单个或者同时写两个触发验证方式
						trigger: 'blur,change'
					}],
					sex: [{
						required: true,
						message: '请选择性别',
						// 可以单个或者同时写两个触发验证方式
						trigger: 'blur,change'
					}],
					sfid: [{
							required: true,
							message: '请输入身份证',
							// 可以单个或者同时写两个触发验证方式
							trigger: 'blur,change'
						},
						{
							len: 18,
							message: '请正确输入18位身份证',
							// 可以单个或者同时写两个触发验证方式
							trigger: 'blur,change'
						}
					],
					phone: [{
						required: true,
						message: '请输入就诊人手机号',
						// 可以单个或者同时写两个触发验证方式
						trigger: 'blur,change'
					},
						{
							len: 11,
							message: '请正确输入11位手机号',
							// 可以单个或者同时写两个触发验证方式
							trigger: 'blur,change'
						}]
				},

			}
		},
		watch: {
			value(newValue, oldValue) {
				//console.log('v-model', newValue);
			}
		},
		computed: {
			...mapState(['token'], ['userInfo']),
			...mapGetters(['hasLogin', 'getUserInfo']),
			getIcon() {
				return path => {
					return 'https://cdn.uviewui.com/uview/example/' + path + '.png';
				}
			}
		},
		onLoad:function(option){
			this.userInfo.PatientId=option.PatientId
			this.getdetail()
		},
		onReady() {
			this.$refs.form1.setRules(this.rules);
		},
		methods: {
			async getdetail(){
				//执行完刷新
				uni.showLoading({
					title: '正在获取个人信息',
					mask: true
				});
				this.$request("hispatient","detail",{
					PatientId:this.userInfo.PatientId
				}).then(res=>{
					if (res.code == 1) {
						let data=res.data
						console.log(res)
						this.userInfo.PatientId=data.PatientId
						this.userInfo.name=data.Name
						this.userInfo.sex=data.Sex
						this.userInfo.sfid=data.IDCard
						this.userInfo.phone=data.Mobile
						this.timeinfor.time=data.Mobile
						this.userInfo.age=data.Age
						let obj=[
							data.Province,
							data.City,
							data.County
						]
						this.adderssinfor.array=obj
						this.adderssinfor.Province=data.Province
						this.adderssinfor.City=data.City
						this.adderssinfor.County=data.County
						this.userInfo.xq=data.HousEstate
						this.userInfo.lh=data.HousBuilding
						this.userInfo.dyh=data.HousUnit
						this.userInfo.fjh=data.HousRoom
						
					}
				})
				uni.hideLoading();
			},
			timePickerChange(e){
				console.log('picker发送选择改变，携带值为', e)
				let obj=e.year+"-"+e.month+"-"+e.day
				// [
				// 	e.year,
				// 	e.month,
				// 	e.day
				// ]
				this.timeinfor.time =obj
			},
			bindPickerChange(e) {
				console.log('picker发送选择改变，携带值为', e)
				let obj=[
					e.province.name,
					e.city.name,
					e.area.name
				]
				this.adderssinfor.array =obj
				this.adderssinfor.Province=e.province.name
				this.adderssinfor.City=e.city.name
				this.adderssinfor.County=e.area.name
				console.log('this.adderssinfor', this.adderssinfor)
			},
			onSelect1(arr) {
				let current = arr[0];
				console.log("current", current)
				let label = current.label;
				this.select1.current = current;
				this.userInfo.sex = label;
				console.log("this.userInfo.sex", this.userInfo.sex)
			},
			submit() {
				this.$refs.form1.validate(valid => {
					if (valid) {
						this.addvisit()
						console.log('验证通过');
					} else {
						console.log('验证失败');
					}
				});
			},
			cancel() {
				this.show = false
			},
			confirm(e) {
				this.sexs = e.value[0]
				this.userInfo.sex = e.value[0]
				this.show = false
			},
			addvisit() {

				console.log(this.userInfo)
				uni.showLoading({
					title: '提交中'
				})
				let user = this.userInfo
				this.$request('hispatient', 'submit', {
					PatientId:this.userInfo.PatientId,
					Name: user.name,
					Sex: user.sex,
					age:user.age,
					IDCard: user.sfid,
					Mobile: user.phone,
					Birthday:this.timeinfor.time,
					Province:this.adderssinfor.Province,
					City:this.adderssinfor.City,
					County:this.adderssinfor.County,
					HousEstate:user.xq,
					HousBuilding:user.lh,
					HousUnit:user.dyh,
					HousRoom:user.fjh
				}).then(res => {
					console.log(res)
					if (res.code == 1) {
						let doctorinfor = false
						uni.hideLoading();
						uni.redirectTo({
							url: `/pages/block/Visit/Visit?doctorinfor=${doctorinfor}`,
							fail(error) {
								console.log(error)
							}
						})
					}else{
						uni.hideLoading();
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				})
			},
			showToast(params) {
				this.$refs.uToast.show({
					...params,
					complete() {
						params.url && uni.navigateTo({
							url: params.url
						})
					}
				})
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				console.log(this.userInfo.phone)
				if (this.userInfo.phone == "") {
					this.showToast(this.list)
					return
				}
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			change(e) {
				console.log('change', e);
			}
		}
	}
</script>

<style>
	@import './addVisit.css'
</style>